/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converters;

import entities.MaterialConstruccion;
import entities.MedidaMaterial;
import entities.Prefijo;
import entities.TipoMaterial;
import entities.TipoVia;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import persistence.MaterialConstruccionJpaController;
import persistence.MedidaMaterialJpaController;
import persistence.PrefijoJpaController;
import persistence.TipoMaterialJpaController;
import persistence.TipoViaJpaController;

/**
 *
 * @author Felipe Duarte
 */
public class TipoViaConverter implements Converter{
    
    TipoViaJpaController tipoViaJpaController;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.equals("")) {
            return null;
        }
        if (tipoViaJpaController == null) {
            tipoViaJpaController = (TipoViaJpaController) context.getApplication().getELResolver().
                    getValue(context.getELContext(), null, "tipoViaJpaController");
        }
        return tipoViaJpaController.findTipoVia(Integer.parseInt(value));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        }
        TipoVia tipoVia = (TipoVia) value;
        return "" + tipoVia.getIdTipoVia();
    }
}